********************************
***Fix Message Vary Source***
********************************

clear all

********************
*Set Person Working*
*Change this to your name!!!!!
********************
*global carolina 0
*global emily 1

global emily 1
global carolina 0

*********************
*Load Files*
*********************

*Carolina's Computer*
if $carolina ==1  {
cap cd "~\Dropbox\Carolina-Emily-Project\Data\Study"
global out "..\..\Results\"
}


if $emily ==1  {
*Emily's Computer*
cap cd "~/Dropbox/SourceContent/Data/Study"
global out "../../Results/"
global draftout "../../Draft/Figures/"
} 

use 3_clean_data, replace


/*Sample KSmirnov Code*
gen groupk=1 if treatment==1
replace groupk=0 if  treatment==0
ksmirnov ln_prob_level_anti  if democrat==0 & (treatment==1 | treatment==0), by(group)

r(p_1) is probability that the first group distribution is less than the second group
r(p_2) is probability that the first group distribution is greater than the second group
r(p) gives combined probability of difference
*/

*********************************
***Create Variables and Format***
*********************************

global lasso0 hispanic age55_64 candidate16_hillary candidate16_other ///
	immlevel_decrease gun_lessstrict abortion_illegal tax_toohigh health_notgvt ///
	occ_twitter occ_buzzfeed trumpfan lebronfan taylorfan bgatesfan ///
	obamafan obama_neutral topissue_health 
	
global lasso1 black hispanic age35_44 age45_54 hsdegree ///
	candidate16_hillary immlevel_decrease immlevel_same ///
	gun_same gun_lessstrict abortion_partlegal abortion_illegal tax_toohigh ///
	health_neutral health_notgvt i.freq_nytimes ///
	daily_tv occ_tv daily_newspaper i.freq_fox week_breitbart ///
	daily_breitbart occ_buzzfeed ///
	lebron_neutral obamafan trump_neutral trumpfan west topissue_tax 

	
/*Order or Treatment Groups*
5 7 6 8 1 3 2 4 9 10
*/


gen group1=treatment==5 | treatment==9 | treatment==0
gen group2=treatment==6 | treatment==10 | treatment==0
gen group3=treatment==1 | treatment==9 | treatment==0
gen group4=treatment==2 | treatment==10 | treatment==0

global message1 "Anti"
global message2 "Anti"
global message3 "Pro"
global message4 "Pro"

global source1 "Trump"
global source2 "Obama"
global source3 "Trump"
global source4 "Obama" 

foreach y in 1 3 {
	global space`y' "[0.1em]" 
	} 
foreach y in 2 {
	global space`y' "[0.8em]" 
	} 
foreach y in 4 {
	global space`y' "[0.8em]" 
	} 
	
gen president=treatment==1 | treatment==2 | treatment==5 | treatment==6
gen actor=treatment==3 | treatment==4 | treatment==7 | treatment==8
gen turkey= treatment==9 | treatment==10
gen priming=turkey==1 | president==1

global color1 "blue"
global color0 "red"

*gen fan_president=obamafan if treatment==6 | treatment==8 | treatment==2 | treatment==4
*replace fan_president=trumpfan if treatment==5 | treatment==7 | treatment==1 | treatment==3


*****************************
*****************************
*****************************
global party0 "Republicans"
global party1 "Democrats"
global var0 "ln(Probability Anti-Immigrant)"
global var1 "ln(Probability Pro-Immigrant)"
global rule0 "\toprule"
global rule1 "\midrule"
global dir0 "anti"
global dir1 "pro"


**************************************************
*DECOMPOSITION I* Source Fixed Vary Content
*CREATE PLOT OF RESULTS *
**************************************************

/*President vs. Actor vs. No-Audio*
4 Messages:
Anti-Immigrant (Trump)
Anti-Immigrant (Obama)
Pro-Immigrant (Trump)
Pro-Immigrant (Obama)
2 Graphs: 
Republicans
Democrats
*/


preserve

foreach p in 0 1 {
	gen b`p'=.
	gen u`p'=.
	gen l`p'=.
}

gen n=_n

foreach p in 0 1 {

local n=0
	
foreach y in 4 3 2 1 {
	
	********************
	*Identified Message*
	********************

	local n=`n'+1
 
	reg ln1_prob_index_${dir`p'} priming president ${lasso`p'} ///
		if (group`y'==1) & recruit==`p', robust  

		replace b`p' = _b[president] if n==`n'
		replace u`p' = _b[president]+1.96*_se[president] if n==`n'
		replace l`p' = _b[president]-1.96*_se[president] if n==`n'

	*Distribution Difference - President vs. Priming*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} if (group`y'==1 & treatment!=0) & recruit==`p'
	predict res, res
	gen g=0 if turkey==1 & e(sample)==1
	replace g=1 if president==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local d`n'`p'=round(r(p)*1000)/1000
	local d`n'`p': di %6.3f `d`n'`p''
	drop g res
	
	********************
	*Priming*
	********************

	local n=`n'+1
	
	reg ln1_prob_index_${dir`p'} priming president ${lasso`p'} ///
		if (group`y'==1) & recruit==`p', robust  

		replace b`p' = _b[priming] if n==`n'
		replace u`p' = _b[priming]+1.96*_se[priming] if n==`n'
		replace l`p' = _b[priming]-1.96*_se[priming] if n==`n'

	*Distribution Difference - Priming vs. Control*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} ///
		if (group`y'==1 & president==0)  & recruit==`p'
	predict res, res
	gen g=0 if priming==0 & e(sample)==1
	replace g=1 if priming==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local d`n'`p'=round(r(p)*1000)/1000
	local d`n'`p': di %6.3f `d`n'`p''
	drop g res
	
	********************
	*Total*
	********************

	local n=`n'+1

	reg ln1_prob_index_${dir`p'} president ${lasso`p'} ///
		if (group`y'==1 & turkey==0) & recruit==`p', robust  

		replace b`p' = _b[president] if n==`n'
		replace u`p' = _b[president]+1.96*_se[president] if n==`n'
		replace l`p' = _b[president]-1.96*_se[president] if n==`n'

	*Distribution Difference - President vs. Control*
	reg ln1_prob_index_${dir`p'} ${lasso`p'} if (group`y'==1 & turkey==0) & recruit==`p'
	predict res, res
	gen g=0 if president==0 & e(sample)==1
	replace g=1 if president==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local d`n'`p'=round(r(p)*1000)/1000
	local d`n'`p': di %6.3f `d`n'`p''
	drop g res
	
	local n=`n'+2
	
	}
	

*Decomposition: Source Fixed - Vary Message*
# delimit ;
twoway	
	(rcap l`p' u`p' n if n==3 | n==8 | n==13 | n==18 , horizontal lcolor(black)) 
	(scatter n b`p'  if n==3 | n==8 | n==13 | n==18, mcolor(black) )	
	(rcap l`p' u`p' n if n==2 | n==7 | n==12 | n==17 , horizontal lcolor(black) ) 
	(scatter n b`p'  if n==2 | n==7 | n==12 | n==17, mcolor(black) msymbol(circle_hollow) )	
	(rcap l`p' u`p' n if n==1 | n==6 | n==11 | n==16 , horizontal lcolor(black) ) 
	(scatter n b`p'  if n==1 | n==6 | n==11 | n==16, mcolor(black) msymbol(square_hollow) )	
	, 
	xline(0, lcolor(black)) 
	ylabel(
	19 "{bf:P(Dist):}"
	18	`" "{bf:Anti}		{it:`d18`p''}" "'
	17	`" "{bf:(Trump)} 	{it:`d17`p''}" "'
	16	`" "{it:`d16`p''}" "'
	15 	" "
	14 	" "
	13	`" "{bf:Anti}		{it:`d13`p''}" "'
	12	`" "{bf:(Obama)} 	{it:`d12`p''}" "'
	11	`" "{it:`d11`p''}" "'
	10 	" "
	9 	" "
	8	`" "{bf:Pro}		{it:`d8`p''}" "'
	7	`" "{bf:(Trump)} 	{it:`d7`p''}" "'
	6	`" "{it:`d6`p''}" "'
	5 	" "
	4 	" "
	3	`" "{bf:Pro}		{it:`d3`p''}" "'
	2	`" "{bf:(Obama)} 	{it:`d2`p''}" "'
	1	`" "{it:`d1`p''}" "'
	, angle(0) labsize(small)) 
	xtitle(" " "Model Coefficients" " ")
	ytitle(" ")
	graphregion(color(white)) 
	legend(order(2 "Total" 4 "Source Priming" 6 "Identified Message") rows(3))
	xsize(4) ysize(5) ylabel(, nogrid tlength(0))
	xscale(range(-.04(.01).04)) xlabel(-.04(.01).04)
	;
	# delimit cr

graph export "$draftout/5_Plot_SourceFixed_VaryMessage_${party`p'}.eps", replace	

}

restore











